unit Connection;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.FB,
  FireDAC.Phys.FBDef, FireDAC.Phys.IBWrapper, FireDAC.VCLUI.Wait,FireDAC.dapt,
  FireDAC.Comp.Client, FireDAC.Phys.IBBase, FireDAC.Comp.UI, Data.DB;

type
  TConnectionDM = class(TDataModule)
    FADConnection: TFDConnection;
    BackupService: TFDIBBackup;
    RestoreService: TFDIBRestore;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    FDPhysFBDriverLink: TFDPhysFBDriverLink;
    FADTransaction: TFDTransaction;
    procedure DataModuleCreate(Sender: TObject);
    procedure FADConnectionBeforeConnect(Sender: TObject);
  private
    { Private declarations }
    FDataBaseName: string;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; databaseName: String); reintroduce;
    destructor Destroy; override;

    procedure StartTransaction;
    procedure Commit;
    procedure Rollback;
    function InTransaction: boolean;

    procedure Backup(filename: String);
    procedure Restore(filename: String);

  end;

var
  ConnectionDM: TConnectionDM;

implementation

{ %CLASSGROUP 'Vcl.Controls.TControl' }

{$R *.dfm}
{ TConnectionDM }

procedure TConnectionDM.Backup(filename: String);
begin
  BackupService.BackupFiles.Clear;
  BackupService.BackupFiles.Add(filename);
  BackupService.Backup;

end;

procedure TConnectionDM.Commit;
begin
  FADConnection.Commit;
end;

constructor TConnectionDM.Create(AOwner: TComponent; databaseName: String);
begin
  inherited Create(AOwner);
  FDataBaseName := databaseName;

end;

procedure TConnectionDM.DataModuleCreate(Sender: TObject);
begin
  FADConnection.Connected := True;

  RestoreService.Database := FADConnection.params.values['Database'];
  RestoreService.UserName := FADConnection.params.values['User_Name'];
  RestoreService.Password := FADConnection.params.values['Password'];

  BackupService.Database := FADConnection.params.values['Database'];
  BackupService.UserName := FADConnection.params.values['User_Name'];
  BackupService.Password := FADConnection.params.values['Password'];

{$IFDEF DEBUG}
  FDPhysFBDriverLink.Embedded := false;
{$ELSE}
  FDPhysFBDriverLink.Embedded := True;
{$ENDIF}
end;

destructor TConnectionDM.Destroy;
begin

  inherited;
end;

procedure TConnectionDM.FADConnectionBeforeConnect(Sender: TObject);
var
  libname: String;
begin
{$IFDEF DEBUG}
  libname := 'fbclient.dll';
{$ELSE}
  libname := ExtractFilePath(Paramstr(0)) + 'fbembed.dll';
{$ENDIF}
  FDPhysFBDriverLink.VendorLib := libname;
  FADConnection.params.values['Database'] := FDataBaseName;

end;

function TConnectionDM.InTransaction: boolean;
begin
  result := FADConnection.InTransaction;
end;

procedure TConnectionDM.Restore(filename: String);
begin
  FADConnection.Connected := false;
  RestoreService.BackupFiles.Clear;
  RestoreService.BackupFiles.Add(filename);
  RestoreService.Restore;

end;

procedure TConnectionDM.Rollback;
begin
  FADConnection.Rollback;
end;

procedure TConnectionDM.StartTransaction;
begin
  FADConnection.StartTransaction;
end;

end.
